package com.vcc.core.util;

import com.vcc.core.exception.ProgramException;
import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Random;

public class AesUtil {
    private static final String KEY_ALGORITHM = "AES";
    private static final String CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";
    private static final int OFFSET = 16;
    private static final String SOURCE_CODES = "0123456789ABCDEF";

    private static final Logger logger = LoggerFactory.getLogger(AesUtil.class);


    public final String getKey() {
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        int var3 = 0;
        for(byte var4 = 15; var3 <= var4; ++var3) {
            sb.append(SOURCE_CODES.charAt(random.nextInt(SOURCE_CODES.length())));
        }
        String var10001 = sb.toString();
        return toHexString(var10001);
    }

    private String toHexString(String s) {
        StringBuilder str = new StringBuilder();
        for(int i = 0; i < s.length(); ++i) {
            char element = s.charAt(i);
            String s4 = Integer.toHexString(element);
            str.append(s4);
        }
        return str.toString();
    }


    public static String encrypt( String content) {
        return encrypt(content, "lbO6mXYJ38SnqsAr");
    }


    public static String decrypt( String content) {
        return decrypt(content, "lbO6mXYJ38SnqsAr");
    }


    public static String encrypt( String content,  String key) {
        String resultString;
        try {
            byte[] keyByteArray = key.getBytes( StandardCharsets.UTF_8);
            SecretKeySpec skey = new SecretKeySpec(keyByteArray, "AES");
            keyByteArray = key.getBytes( StandardCharsets.UTF_8);
            IvParameterSpec iv = new IvParameterSpec(keyByteArray, 0, 16);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            byte[] byteContent = content.getBytes( StandardCharsets.UTF_8);
            cipher.init(1, skey, iv);
            resultString = new Base64().encodeToString(cipher.doFinal(byteContent));
        } catch (Exception var9) {
            logger.error("加密失败");
            resultString = "";
        }
        return resultString;
    }

    public static String decrypt( String content,  String key) {
        String resultString;
        try {
            byte[] keyByteArray = key.getBytes( StandardCharsets.UTF_8);
            SecretKeySpec skey = new SecretKeySpec(keyByteArray, "AES");
            keyByteArray = key.getBytes( StandardCharsets.UTF_8);
            IvParameterSpec iv = new IvParameterSpec(keyByteArray, 0, 16);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(2, skey, iv);
            byte[] result = cipher.doFinal(new Base64().decode(content));
            resultString = new String(result,  StandardCharsets.UTF_8);
        } catch (Exception var8) {
            logger.error("解密失败");
            resultString = "";
        }
        return resultString;
    }


    public static String encrypt( byte[] content,  byte[] password) {
        try {
            SecretKeySpec key = new SecretKeySpec(password, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(1, key);
            return new Base64().encodeToString(cipher.doFinal(content));
        } catch (Exception e) {
            throw new ProgramException(e.getMessage());
        }
    }

    public static String decrypt( byte[] content,  byte[] password) {
        try {
            SecretKeySpec key = new SecretKeySpec(password, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(2, key);
            return new String( cipher.doFinal(content), StandardCharsets.UTF_8);
        } catch (Exception e) {
            throw new ProgramException(e.getMessage());
        }
    }
}
